<?php
/**
 * 操作员管理
 *
 * @author     chenfenghua<843958575@qq.com>
 * @copyright  Copyright 2008-2013 mall.yiitian.com
 * @version    1.0
 */

class UserController extends BaseController
{
    /**
     * 操作员列表
     */
    public function actionIndex()
    {
        $search['name'] = $this->get('name');
        $conditionArr = array();
        $condition = '';
        if ($search['name']) $conditionArr[] = "t.name = '{$search['name']}'";

        if ($conditionArr) $condition = implode(' AND ',$conditionArr);

        $criteria = new CDbCriteria();
        $criteria->select = 't.*,p.login_name';
        $criteria->join = 'LEFT JOIN {{pam_account}} p ON t.user_id = p.account_id';
        $criteria->condition = $condition;

        $user_list = Desktopusers::model()->findAll($criteria);

        $gridDataProvider = new CArrayDataProvider(
            $user_list,
            array(
                'keyField'=>'user_id',
                'sort'=>array(
                    'attributes'=>array(
                        'user_id',
                    ),
                    'defaultOrder'=>array(
                        'role_id'=>CSort::SORT_DESC,
                    )
                ),

                'pagination'=>array(
                    'pageSize'=>$this->pagesize,
                ),

            )
        );
        $template = $this->TemplateButton(array('update','delete'));
        $this->render('index',array('dataProvider'=>$gridDataProvider,'template'=>$template,'search'=>$search));
    }

    /**
     * 权限选择
     */
    public function actionUpdate()
    {
        $user_id = $_GET['user_id'];
        $user_row = Desktopusers::model()->find("user_id = $user_id");
        $role = Desktophasrole::model()->find("user_id=$user_id");
        $user_row['role'] = $role['role_id'];
        if ($_POST) {
            $UserAttr= $_POST['User'];

            if(empty($UserAttr['password'])){
                $user_array = array(
                    'name'=>$UserAttr['name'],
                    'status'=>$UserAttr['status'],
                );
            }else{
                $user_array = array(
                    'name'=>$UserAttr['name'],
                    'status'=>$UserAttr['status'],
                    'password'=>md5(md5($UserAttr['password'])),
                );
            }

            if ($role)
                Desktophasrole::model()->updateAll(array('role_id'=>$UserAttr['role']),
                'user_id=:user_id',array(':user_id'=>$user_id));
            else {
                $hasRole = new Desktophasrole();
                $hasRole->user_id = $user_id;
                $hasRole->role_id = $UserAttr['role'];
                $hasRole->insert();
            }
            Desktopusers::model()->updateByPk($user_id, $user_array);
            $this->redirect('?r=desktop/user/update&user_id='.$user_id.'&save=update&result=success');
//            if () {
//
//            } else {
//                $this->redirect('?r=desktop/user/update&user_id='.$user_id.'&save=update&result=fail');
//            }
        }
        $role_list = Desktoproles::model()->findAll();

        $param['save'] = $param['result'] = '';
        if (isset($_GET['save'])) $param['save'] = $_GET['save'];
        if (isset($_GET['result'])) $param['result'] = $_GET['result'];

        $model['user_row'] = $user_row;
        $model['role_list'] = $role_list;
        $model['param'] = $param;
        $this->render('update',array('model'=>$model));
    }

    /**
     * 新增会员
     *
     * @throws CDbException
     */
    public function actionInsert(){
        if($_POST){
//        var_dump($_POST);die();

            $user = new Desktopusers();
            $user->name = $_POST['User']['name'];
//            $user->role = $_POST['User']['role'];
            $user->password =md5(md5( $_POST['User']['password']));
            $user->status = $_POST['User']['status'] ;
            $user->super = $_POST['User']['super'] ;
            $user->disabled = 'false' ;

            if($user->insert()){
                $user_id = $user->attributes['user_id'];

                $hasRole = new Desktophasrole();
                $hasRole->user_id = $user_id;
                $hasRole->role_id = $_POST['User']['role'];

                $hasRole->insert();

                $this->redirect('?r=desktop/user/update&user_id='.$user_id.'&save=update&result=success');
            }
        }


        $model = array();
        $param = array();
        $model['param']['save']= $model['param']['result'] =$param;
        $model['role_list'] =  Desktoproles::model()->findAll();
        $model['user_row']=$param;
        $this->render('_userform',array('model'=>$model));

    }

    /**
     * 修改密码
     */
    public function actionReset()
    {
        if ($_POST) {
            $user_id = Yii::app()->session['_admini']['user_id'];
            $user_array = $this->post('User');
            $user_array['password'] = md5(md5(trim($user_array['password'])));
            if (Desktopusers::model()->updateByPk(  $user_id, $user_array)) {
                $this->redirect('/');
            } else {
                $this->redirect('/');
            }
        }
        $this->render('reset');
    }
}